
public class BinarySearchTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    public TreeNode root ;

    public boolean insert(int key){
        if(root==null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        TreeNode node = new TreeNode(key);
        while(cur!=null){
            if(cur.val>key){
                parent = cur;
                cur = cur.left;
            }else if(cur.val<key){
                parent = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }
        //此时cur为空节点开始插入节点
        if(parent.val>key){
            parent.left = node;
        }else {
            parent.right = node;
        }
        return true;
    }

    public TreeNode search(int key){
        if(root==null) return null;
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur!=null){
            if(cur.val>key){
                parent = cur;
                cur = cur.left;
            }else if(cur.val<key){
                parent = cur;
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }

    //节点的删除

    public void remove(int key){
        if(root==null) return;
        //首先先找到key
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur!=null){
            if(cur.val>key){
                parent = cur;
                cur = cur.left;
            }else if(cur.val<key){
                parent = cur;
                cur = cur.right;
            }else {
                //找到要删除的结点了
                removeNode(cur,parent);
                return;
            }
        }
    }


    public void removeNode(TreeNode cur,TreeNode parent){
        if(cur.left==null){
            if(cur==root){
                root = root.right;
            }else if(parent.right==cur){
                parent.right = cur.right;
            }else {
                parent.left = cur.right;
            }
        }else if(cur.right==null){
            if(cur==root){
                root = root.left;
            }else if(parent.left==cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //都不等于null，采用替换法
            //去左树中找最大的结点
            TreeNode targetParent = cur;
            TreeNode target = cur.left;
            //左树的最大值是最右边的节点
            while(target.right!=null){
                targetParent=target;
                target = target.right;
            }
            cur.val = target.val;
            if(targetParent.left==target){
                targetParent.left = target.left;
            }else {
                targetParent.right = target.left;
            }
        }
    }



}
